﻿using Edu.Abstracts;
using Edu.Contracts;
using Edu.Entitys;
using Edu.Pages;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Edu.Web.Pages.StudyTask
{
    public class EditModalModel : EduPageModelBase
    {
        [HiddenInput]
        [BindProperty(SupportsGet = true)]
        public Guid Id { get; set; }

        [BindProperty]
        public StudyTaskCreateUpdateDto CreateUpdateDto { get; set; }

        private IStudyTaskCrudAppService _appService;
        private IAppUserRepository appUsers;

        public EditModalModel(IStudyTaskCrudAppService appService, IAppUserRepository appUsers)
        {
            _appService = appService;
            this.appUsers = appUsers;
        }

        public async Task OnGet()
        {
            var appDto = await _appService.GetAsync(Id);
            CreateUpdateDto = ObjectMapper.Map<StudyTaskDto, StudyTaskCreateUpdateDto>(appDto);
            ObjectMapper.Map<StudyTaskContent, StudyTaskCreateUpdateDto>(appDto.Content, CreateUpdateDto);

            var teacher = await appUsers.GetAsync(appDto.TeacherId);

            var student = await appUsers.GetAsync(appDto.StudentId);

            CreateUpdateDto.StudentPhoneNum = student.PhoneNumber;
            CreateUpdateDto.TeacherPhoneNum = teacher.PhoneNumber;
        }


        public async Task<IActionResult> OnPostAsync()
        {
            await _appService.UpdateAsync(Id, CreateUpdateDto);
            return NoContent();
        }
    }
}